//
//  StatusCell.swift
//  wbxm
//
//  Created by apple on 15/12/18.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit
import SnapKit
//cell = 顶部视图 + 转发微博视图 + 底部视图

class StatusCell: UITableViewCell {
    //自定义约束属性
    var bottomViewTopConstraints: Constraint?

    //定义微博模型属性
    var status: Status? {
        didSet{
            //给顶部视图 设置数据
            topVeiw.status = status
            

            //卸载掉之前的顶部约束
            self.bottomViewTopConstraints?.uninstall()
            //根据模型 提供的数据来判断是否显示转发微博视图
            if let retweetedStatus = status?.retweeted_status {
                //有转发微博
                //修改约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                
                //给转发微博视图 设置数据
                retweetedView.retweetedStatus = retweetedStatus
                retweetedView.hidden = false

            }else{
                //没有转发微博
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                   self.bottomViewTopConstraints = make.top.equalTo(topVeiw.snp_bottom).constraint
                })
                retweetedView.hidden = true
            }
            
        }
    }
    
    //cell 入口
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        //设置cell的选中样式
        selectionStyle = .None
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //自定义cell
    private func setupUI() {
    
        //使用contenView 添加子视图
        contentView.addSubview(topVeiw)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
        
        
        //设置约束
        topVeiw.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
            //设置高
//            make.height.equalTo(50)
        }
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(topVeiw.snp_bottom)
//            make.height.equalTo(50)
        }
        
        //设置底部视图的约束
        bottomView.snp_makeConstraints { (make) -> Void in
           self.bottomViewTopConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.left.right.equalTo(self)
            make.height.equalTo(40)
        }
        
        //给contentView设置约束  非常关键的一点
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
        
    }
    
    //懒加载top视图
    private lazy var topVeiw: StatusCellTopView = StatusCellTopView()

    //懒加载底部视图
    private lazy var bottomView: StatusBottomView = StatusBottomView()
    
    //懒加载转发微博视图
    private lazy var retweetedView: StatusRetweetedView = StatusRetweetedView()
}
